//
//  TestViewController.m
//  Oc_Ui_Noob_XZL_Locke
//
//  Created by 江山 on 15/11/15.
//  Copyright © 2015年 hi. All rights reserved.
//

#import "TestViewController.h"
#import "ViewController.h"
#import "Test2ViewController.h"

@interface TestViewController ()

{
    NSArray * data;
    NSArray * detail;
    NSArray * headers;
}

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];

       data = @[
                @[@"安娜",@"安全"],
                @[@"白启",@"白豆腐"],
                @[@"曹操",@"曹植"],
             ];
    
    detail = @[
               @"1",
               @"2",
               @"3",
               ];
    headers = @[
                @"A",
                @"B",
                @"C"
                ];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"第二个控制器";
}

//告诉表格有几个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return data.count;
}

//告诉表格data这个section有几行
- (NSInteger)tableView:(UITableView *)tableVie numberOfRowsInSection:(NSInteger)section
{
    return [data[section]count];
}

//这个方法返回的是每一个行的布局，数据也要在这里面绑定，如果要自定义布局，则这个方法返回自定义的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    cell.textLabel.text = data [indexPath.section][indexPath.row];
    
     cell.detailTextLabel.text = detail[indexPath.row];
    
    //每行右边的箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    


    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return headers[section];
}

//验证响应选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ViewController *con = [ViewController new];
    [self.navigationController pushViewController:con animated:YES];
}

//让行可移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    
}

//对编辑状态下提交的事件响应(删除操作)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray *arr = data.mutableCopy;
     
        NSMutableArray * inArr =[data[indexPath.section]mutableCopy];
        [inArr removeObjectAtIndex:indexPath.row];
        
        [arr replaceObjectAtIndex:indexPath.section withObject:inArr];
        data = arr;
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
